package besteAukera;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;



public class DatuMota extends JFrame{
	private static final long serialVersionUID = 1L;
	private JButton positiboa = null;
	private JButton birakoOs = null;
	static DatuMota datuMota=null;

	private String posButton, birakoButton,azaleraGaizki,altuera,zabalera,tamainaIzenburua,datuMotaIzenburua;
@SuppressWarnings("unused")
private  String hizkuntza,cancelBotoia, onartuBotoia, gordeBotoia,irekiBotoia, gordeToolTip,cancelToolTip,fitxategiIzenaLabel,fitxategiMotaLabel,xehetasunTooltip,zerrendatuToolTip,mailaGoraToolTip,berriaKarpToolTip,homeToolTip,tamainaText,motaText,dataText,atributuakText,gordeNon,izenaText,aldatuaText,irekiText,irekiLeihoa,gordeLeihoa,irekiBotoiaText;
public DatuMota(String hizk){
	boolean aurkitua=false;
	datuMota=this;
	hizkuntza=hizk;
	this.setIconImage(Toolkit.getDefaultToolkit().getImage("irudiak/upv.gif"));
	File hizki=new File("Hizkuntza/" + hizkuntza + ".txt");	
	if (hizki.exists()){
		try {
			BufferedReader entrada = new BufferedReader( new FileReader( hizki ) );
			while(!aurkitua){
				if(entrada.readLine().compareTo("//DatuMota")==0){
					posButton=entrada.readLine();
					birakoButton=entrada.readLine();
					//azalera errorea datu motarengatik
					azaleraGaizki=entrada.readLine();
					//datuak tamainarentzako
					tamainaIzenburua=entrada.readLine();
					altuera=entrada.readLine();
					zabalera=entrada.readLine();
					datuMotaIzenburua=entrada.readLine();
					
					cancelBotoia=entrada.readLine();
					onartuBotoia=entrada.readLine();
					gordeBotoia=entrada.readLine();
					
					irekiBotoia=entrada.readLine();
					gordeToolTip=entrada.readLine();
					cancelToolTip=entrada.readLine();
					fitxategiIzenaLabel=entrada.readLine();
					fitxategiMotaLabel=entrada.readLine();
					xehetasunTooltip=entrada.readLine();
					zerrendatuToolTip=entrada.readLine();
					mailaGoraToolTip=entrada.readLine();
					berriaKarpToolTip=entrada.readLine();
					homeToolTip=entrada.readLine();
					izenaText=entrada.readLine();
					tamainaText=entrada.readLine();
					aldatuaText=entrada.readLine();
					motaText=entrada.readLine();
					dataText=entrada.readLine();
					atributuakText=entrada.readLine();
					gordeNon=entrada.readLine();
					irekiText=entrada.readLine();
					irekiLeihoa=entrada.readLine();
					gordeLeihoa=entrada.readLine();
					irekiBotoiaText=entrada.readLine();
					aurkitua=true;
			}
				
				}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	JFileChooser f=new  JFileChooser();
	UIManager.put("OptionPane.cancelButtonText",cancelBotoia);
	UIManager.put("OptionPane.okButtonText",onartuBotoia);
UIManager.put("FileChooser.cancelButtonText",cancelBotoia);
	UIManager.put("FileChooser.saveButtonText",gordeBotoia);
	UIManager.put("FileChooser.openButtonText",irekiBotoia);
	UIManager.put("FileChooser.saveButtonToolTipText",gordeToolTip);
	UIManager.put("FileChooser.cancelButtonToolTipText",cancelToolTip);
	UIManager.put ("FileChooser.fileNameLabelText", fitxategiIzenaLabel);
    UIManager.put ("FileChooser.filesOfTypeLabelText", fitxategiMotaLabel);
    UIManager.put ("FileChooser.detailsViewButtonToolTipText", xehetasunTooltip);
    UIManager.put ("FileChooser.listViewButtonToolTipText", zerrendatuToolTip);
    UIManager.put ("FileChooser.upFolderToolTipText", mailaGoraToolTip);
    UIManager.put ("FileChooser.newFolderToolTipText", berriaKarpToolTip);
    UIManager.put ("FileChooser.homeFolderToolTipText", homeToolTip);
    UIManager.put ("FileChooser.fileNameHeaderText", izenaText);

    UIManager.put ("FileChooser.fileSizeHeaderText", tamainaText);
    UIManager.put ("FileChooser.fileTypeHeaderText", motaText);
    UIManager.put ("FileChooser.fileDateHeaderText", aldatuaText);
    UIManager.put ("FileChooser.fileAttrHeaderText", atributuakText);
    UIManager.put ("FileChooser.saveInLabelText", gordeNon);
    UIManager.put ("FileChooser.lookInLabelText", irekiText);
    UIManager.put ("FileChooser.openDialogTitleText", irekiLeihoa);
    UIManager.put ("FileChooser.saveDialogTitleText", gordeLeihoa);
    UIManager.put("FileChooser.openButtonToolTipText",irekiBotoiaText);
    
    SwingUtilities.updateComponentTreeUI(f);
    
 	DatuMotaFondoa botoiakPanel=new DatuMotaFondoa("");
	
	DatuMotaFondoa northPanel=new DatuMotaFondoa("irudiak/north1.png");
	northPanel.setLayout(null);
	northPanel.setPreferredSize(new Dimension(300,50));
	DatuMotaFondoa southPanel=new DatuMotaFondoa("irudiak/south1.png");
	southPanel.setLayout(null);
	southPanel.setPreferredSize(new Dimension(300,50));
	DatuMotaFondoa eastPanel=new DatuMotaFondoa("irudiak/east1.png");
	eastPanel.setLayout(null);
	eastPanel.setPreferredSize(new Dimension(150,50));
	DatuMotaFondoa westPanel=new DatuMotaFondoa("irudiak/west1.png");
	westPanel.setLayout(null);
	westPanel.setPreferredSize(new Dimension(150,50));
	this.setTitle(datuMotaIzenburua);
	
	botoiakPanel.setLayout(new GridLayout());
	positiboa=new JButton(posButton);
	positiboa.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
			datuMota.setVisible(false);
			/////////////////////////////////////
			  JTextField zabalera = new JTextField();		 
              JTextField altuera = new JTextField();
             
              Object[] message = {DatuMota.this.zabalera, zabalera, 
                      DatuMota.this.altuera, altuera};
              JOptionPane pane = new JOptionPane( message, 
                                              JOptionPane.PLAIN_MESSAGE, 
                                              JOptionPane.OK_CANCEL_OPTION);
              JDialog tam=pane.createDialog(null, tamainaIzenburua);
              tam.setVisible(true);
             tam.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
              Object aukera=pane.getValue();
              if(aukera!=null){
              Boolean zenb2=new KonprobatuOsokoa().verify(zabalera);
              Boolean zenb1=new KonprobatuOsokoa().verify(altuera);
              if(aukera.equals(new Integer(JOptionPane.CANCEL_OPTION))){
            	  datuMota.setVisible(true);
              }
              if(aukera.equals(new Integer(JOptionPane.CANCEL_OPTION))){
            	  datuMota.setVisible(true);
              }
               if ((aukera.equals(new Integer(JOptionPane.OK_OPTION)))){
            	  if((zenb1)&&(zenb2)){
            		  
                      String dimentsioa= zabalera.getText()+" - "+altuera.getText();
                      String[] tamaina=dimentsioa.split(" - ");

        	////////////////////////////
        			Nagusia nag=new Nagusia("Positiboa",hizkuntza,Integer.parseInt(tamaina[1]),Integer.parseInt(tamaina[0]),datuMotaIzenburua);
        			nag.setLocationRelativeTo(null);
        			nag.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        			nag.setVisible(true); 
        		 

            	  }
            	  else{
            		  JOptionPane.showMessageDialog(null, azaleraGaizki);
                      DatuMota b=new DatuMota(hizkuntza);
                      b.setLocationRelativeTo(null);
                      b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      b.setVisible(true);
                     
            	  	}
            
              }}
              else {
            	  DatuMota b=new DatuMota(hizkuntza);
            	  b.setLocationRelativeTo(null);
                  b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  b.setVisible(true);
             
              }
             }
	});
	birakoOs=new JButton(birakoButton);
	birakoOs.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

			datuMota.setVisible(false);
			/////////////////////////////////////
			  JTextField zabalera = new JTextField();
			 
			 
              JTextField altuera = new JTextField();
             
              Object[] message = {DatuMota.this.zabalera, zabalera, 
                      DatuMota.this.altuera, altuera};
             
              JOptionPane pane = new JOptionPane( message, 
                                              JOptionPane.PLAIN_MESSAGE, 
                                              JOptionPane.OK_CANCEL_OPTION);
              pane.createDialog(null, tamainaIzenburua).setVisible(true);
             
              Object aukera=pane.getValue();
              
              Boolean zenb2=new KonprobatuOsokoa().verify(zabalera);
              Boolean zenb1=new KonprobatuOsokoa().verify(altuera);
              DatuMota b = null;
              if(aukera!=null){
            	  if(aukera.equals(new Integer(JOptionPane.CANCEL_OPTION))){
                	  datuMota.setVisible(true);
                  }
            	  else if ((aukera.equals(new Integer(JOptionPane.OK_OPTION)))){
            	 
            	  if((zenb1)&&(zenb2)){
            		  
                      String dimentsioa= zabalera.getText()+" - "+altuera.getText();
                      String[] tamaina=dimentsioa.split(" - ");

        	////////////////////////////
        			
        		      
        			Nagusia nag=new Nagusia("BirakoOs",hizkuntza,Integer.parseInt(tamaina[1]),Integer.parseInt(tamaina[0]),datuMotaIzenburua);
        			nag.setLocationRelativeTo(null);
        			nag.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        			nag.setVisible(true);
            	  }
            	  else{
            		  JOptionPane.showMessageDialog(null, azaleraGaizki);
                      b=new DatuMota(hizkuntza);
                      b.setLocationRelativeTo(null);
                      b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      b.setVisible(true);
            	  	}
            
              }
              }
              else {
            	   b=new DatuMota(hizkuntza);
            	   b.setLocationRelativeTo(null);
            	   b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   b.setVisible(true);
     		
                 
             
              }
             
		}
	});
	botoiakPanel.add(positiboa);
	botoiakPanel.add(birakoOs);

	this.getContentPane().setLayout(new BorderLayout());
	this.getContentPane().add(northPanel, BorderLayout.NORTH);
	this.getContentPane().add(southPanel, BorderLayout.SOUTH);
	this.getContentPane().add(westPanel, BorderLayout.WEST);
	this.getContentPane().add(eastPanel, BorderLayout.EAST);
	this.getContentPane().add(botoiakPanel, BorderLayout.CENTER);
	
	this.pack();
	
}
}
